<?php
namespace app\admin\controller;

use app\admin\model\WebsetModel;
use think\Request;
use think\Db;

class Webset extends Base
{
	public function index(){
		return $this->fetch();
	}

	public function subForm(){
		$params = input('post.');
		$websets = Db::name('webset')->select();
		$configurl = __DIR__.'../../../config.php';
		DB::startTrans();
		try{
			foreach($websets as $v){
				foreach($params as $key => $va){
					if($v['type'] == 0 && $v['name'] == $key){
						DB::name('webset')->where('id',$v['id'])->update(['values'=>$va]);
					}
					if($v['type'] == 2 && $v['name'] == 'app_debug' && $key == 'app_debug'){
						$app_debug = DB::name('webset')->where('name','app_debug')->value('values');
						$origin_str = file_get_contents($configurl);
						$update_str = str_replace("'app_debug' => ".$app_debug, "'app_debug' => ".$va, $origin_str);
						file_put_contents($configurl, $update_str);
						DB::name('webset')->where('id',$v['id'])->update(['values'=>$va]);
						$app_trace = DB::name('webset')->where('name','app_trace')->value('values');
						$origin_str = file_get_contents($configurl);
						$update_str = str_replace("'app_trace' => ".$app_trace, "'app_trace' => ".$va, $origin_str);
						file_put_contents($configurl, $update_str);
						DB::name('webset')->where('id',$v['id'])->update(['values'=>$va]);
					}
					if($v['type'] == 2 && $v['name'] == 'max_size_of_upload' && $key == 'max_size_of_upload'){
						$size = DB::name('webset')->where('name','max_size_of_upload')->value('values');
						$origin_str = file_get_contents($configurl);
						$update_str = str_replace("'max_size_of_upload' => ".$size, "'max_size_of_upload' => ".$va, $origin_str);
						file_put_contents($configurl, $update_str);
						DB::name('webset')->where('id',$v['id'])->update(['values'=>$va]);
					}
					if($v['type'] == 2 && $v['name'] == 'ext' && $key == 'ext'){
						$ext = DB::name('webset')->where('name','ext')->value('values');
						$ext = explode('-',$ext);
						$ext_str = '[';
						foreach($ext as $xx => $ee){
							if($xx == (sizeof($ext)-1)){
								$ext_str .= "'".$ee."']";
							}else{
								$ext_str .= "'".$ee."',";
							}
						}
						$va_arr = explode('-',$va);
						$va_str = '[';
						foreach($va_arr as $xx => $ee){
							if($xx == (sizeof($va_arr)-1)){
								$va_str .= "'".$ee."']";
							}else{
								$va_str .= "'".$ee."',";
							}
						}
						$origin_str = file_get_contents($configurl);
						$update_str = str_replace("'ext' => ".$ext_str, "'ext' => ".$va_str, $origin_str);
						file_put_contents($configurl, $update_str);
						DB::name('webset')->where('id',$v['id'])->update(['values'=>$va]);
					}
				}
			}
			DB::commit();
			return ['code'=>1,'msg'=>'保存成功'];
		}catch(Exception $e){
			DB::rollback();
			return ['code'=>0,'msg'=>'保存失败'.$e->getMessage()];
		}

	}

}
